package cn.itcast.controller;

import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/anno")
@SessionAttributes(value = {"msg"})
public class annoController implements Serializable {

    /**
     * RequestParam注解
     * @param username
     * @return
     */
    @RequestMapping("/testRequestParam")
    public  String testRequestParam(@RequestParam("uname") String username){
        System.out.println("testRequestParam");
        System.out.println(username);
        return "success";
    }

    /**
     * RequestBody注解
     * @param body
     * @return
     */
    @RequestMapping("/testRequestBody")
    public  String testRequestBody(@RequestBody String body){
        System.out.println("testRequestBody");
        System.out.println(body);
        return "success";
    }


    /**
     * PathVaribale注解
     * @return
     */
    @RequestMapping("/testPathVaribale")
    public  String testPathVaribale(  ){
        System.out.println("testPathVaribale123");
        return "success";
    }
    /**
     * PathVaribale注解
     * @return
     */
    @RequestMapping("/testPathVaribale/{sid}")
    public  String testPathVaribale(@PathVariable(name = "sid") int id){
        System.out.println("testPathVaribale");
        System.out.println(id);
        return "success";
    }

    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request, HttpServletResponse response){
        System.out.println("testServletAPI");

        System.out.println(request);

        HttpSession session = request.getSession();
        System.out.println(session);

        ServletContext servletContext = session.getServletContext();
        System.out.println(servletContext);

        System.out.println(response);
        return "success";

    }

    @RequestMapping("testRequsetHearder")
    public String testRequsetHearder(@RequestHeader(value = "Accept") String header){
        System.out.println("testRequsetHearder");
        System.out.println(header);
        return "success";
    }

    @RequestMapping("testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue){
        System.out.println("testCookieValue");
        System.out.println(cookieValue);
        return "success";
    }


    @RequestMapping("testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println("testModelAttribute");
        System.out.println(user);
        return "success";
    }

//    @ModelAttribute
//    public void   showUser(String uname,Map<String,User> map ){
//        System.out.println("showUser......");
//        User user = new User();
//        user.setUname(uname);
//        user.setAge(20);
//        user.setDate(new Date());
//        map.put("abc",user);
//    }


//    @ModelAttribute
//    public User showUser(String uname){
//        System.out.println("showUser......");
//        User user = new User();
//        user.setUname(uname);
//        user.setAge(20);
//        user.setDate(new Date());
//        return user;
//    }


    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("testSessionAttributes");
        model.addAttribute("msg","美美");
        return "success";
    }


    @RequestMapping("/testGetSessionAttributes")
    public String testGetSessionAttributes(ModelMap modelMap){
        System.out.println("testGetSessionAttributes");
        String msg = (String) modelMap.get("msg");
        System.out.println(msg);

        return "success";
    }
    @RequestMapping("/testCleanSessionAttributes")
    public String testCleanSessionAttributes(SessionStatus status){
        System.out.println("testGetSessionAttributes");
        status.setComplete();
        return "success";
    }
}
